<?php
require_once 'includes/image_resizer.php'; 

$smsg="";
if($_SESSION['user_id_sess']=="") {
	echo "<script type='text/javascript'> window.location='index.php?signin'; </script>";
}
$userId = $_SESSION['user_id_sess'];
$userType = $_SESSION['user_type_sess'];

if(!empty($_REQUEST['keyword']))
{
	$curProId = $_REQUEST['keyword'];
	$today=date("Y-m-d G:i:s",time());
	
	$pro_quantity=$_REQUEST['pro_quantity'];
	$pname=$_REQUEST['pname'];
	$price=$_REQUEST['price'];	
	$product_code=$_REQUEST['product_code'];	
	$category="";
	
	//Om
	$counter=count($_REQUEST['code']);
	$items_code=array();
	$items_code=$_REQUEST['code'];
	
	$counter=count($items_code);
	
	for($k=0;$k<$counter;$k++)
	{
		if($counter-1==$k)
		{
			$strIDs .="'".$items_code[$k]."'";
		}
		else
		{
			$strIDs .="'".$items_code[$k]."',";
		}
	}
	
	//$sqlInsItem = "delete from tbl_items_code where product_id='$curProId' and isSold='0' and isWhole='1' and code not in ($strIDs) ";				
	
	$sqlInsItem = "delete from tbl_items_code where product_id='$curProId' and isSold='0' and code not in ($strIDs) ";				
	
	$rstInsItem = mysql_query($sqlInsItem);	
	
	for($j=0;$j<$counter;$j++)
	{
		$sqlSelCodes = "SELECT * FROM tbl_items_code WHERE product_id='$curProId' and code='".$items_code[$j]."' ";
		$rstSelCodes = mysql_query($sqlSelCodes);
		$rowSelCodes = mysql_fetch_array($rstSelCodes);
		if(!is_array($rowSelCodes))
		{
			$sqlInsItem = "INSERT INTO tbl_items_code(code,product_id,item_id,owner_id,isSold,sell_by,isWhole) VALUES ";
			if($items_code[$j]!="")
			{
				$sqlInsItem .="('".$items_code[$j]."','$curProId','0',".$_SESSION['user_id_sess'].",'0',0,'1')";			
			}
			else
			{
				$sqlInsItem .="(".rand(1000000000,9999999999).",'$curProId','0',".$_SESSION['user_id_sess'].",'0',0,'1')";		
			}
			$rstInsItem = mysql_query($sqlInsItem);			
		}		
	}
	//$list=implode(",",$items_code);
	
	
	//Om
	
	$category=$_REQUEST['post_category'];

	$pro_description=$_REQUEST['pro_description'];
	$localPickup=$_REQUEST['localPickup'];
	$shipStatus=$_REQUEST['ship_status'];
	
	if($shipStatus) {
		$shipping 	= 	"Free";
	}
	else {
		$shipping 	= 	$_REQUEST['pro_ship'];
	}
	
	
	
	$sql_addpro="UPDATE tbl_wholeseller_product set 
				category_id='$category',
				quantity='$pro_quantity',
				shipping='$shipping',
				localPickup='$localPickup',
				product_name='$pname',
				product_code='$product_code',
				product_price='$price',
				product_description='$pro_description' 					
				where product_id='$curProId'";
	$rst_addpro=mysql_query($sql_addpro);
	//check product entry in tbl_product_images
	$sqlChk = "SELECT product_id FROM tbl_wholeproduct_images WHERE product_Id='$curProId' AND  product_type='p'";
	$rstChk = mysql_query($sqlChk) or die(mysql_error());
	if(mysql_num_rows($rstChk)==0) {
		$sqlInsEntry = "INSERT INTO tbl_wholeproduct_images SET product_id='$curProId', product_type='p'";
		$rstInsEntry = mysql_query($sqlInsEntry) or die(mysql_error()); 
	}
	
	if($rst_addpro) {
		$smsg="true";
		$pro_id=$curProId;
		
		$imgPaths = getWholeImagePath($pro_id); 
		
		for($i=1; $i<=5;$i++) {
			$pro_image[$i]=pathinfo($_FILES["pro_image".$i]["name"]);	
					
			if(!empty($pro_image[$i]['extension'])) {
				if(strtolower($pro_image[$i]['extension'])=="jpeg" || strtolower($pro_image[$i]['extension'])=="jpg" || strtolower($pro_image[$i]['extension'])=="gif") {
					
					/* if($imgPaths!="") {															
							if($imgPaths['product_image_'.$i]!="") { unlink($imgPaths['product_image_'.$i]); }
							if($imgPaths['product_sample_'.$i]!="") { unlink($imgPaths['product_sample_'.$i]); }
							if($imgPaths['product_thumbnail_'.$i]!="") { unlink($imgPaths['product_thumbnail_'.$i]); }
						
					} */
						
					$orgname[$i]=mt_rand();				
					$type[$i]=$pro_image[$i]['extension'];				
					$path[$i]="productswholeseller/original/".$orgname[$i].$pro_id.".".$type[$i];								
					
					$path_thumb[$i]="productswholeseller/thumb/".$orgname[$i].$pro_id."_thumb.".$type[$i];						
					
					$path_medium[$i]="productswholeseller/medium/".$orgname[$i].$pro_id."_medium.".$type[$i];				
					
					move_uploaded_file($_FILES["pro_image".$i]["tmp_name"],$path[$i]);				  
					productResize($path[$i], $path_thumb[$i], $path_medium[$i], $type[$i]);	
					$imagePath[] = 	$path[$i];	
					$mediumPath[] = $path_medium[$i];	
					$thumbPath[] = 	$path_thumb[$i];	
					$sql_updpro="UPDATE tbl_wholeproduct_images set list_type='1',					
								 product_thumbnail_$i='".$path_thumb[$i]."',
								 product_sample_$i='".$path_medium[$i]."',
								 product_image_$i='".$path[$i]."'
								 where product_id=$pro_id AND product_type='p'";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()>0) {
						
					}
					else { $smsg="false"; }		
				}
			 }
		 } //end of for
	}
	else { $smsg="false"; }	
}


$proId = $_GET['editwholeitem'];
$owner = getProductOwner($proId);


$sqlSelPro = "SELECT * FROM tbl_wholeseller_product as tp left join tbl_product_images as tpi ON tp.product_id=tpi.product_id where tp.product_id='$proId'";
$rstSelPro = mysql_query($sqlSelPro);
$rowSelPro = mysql_fetch_array($rstSelPro);
$cId = getCategory($rowSelPro['category_id']);

//Om
$sqlSelCodes = "SELECT GROUP_CONCAT(code SEPARATOR ',' ) AS Codes,GROUP_CONCAT(isSold SEPARATOR ',' ) AS Sold FROM tbl_items_code WHERE product_id='$proId' order by code_id asc ";
$rstSelCodes = mysql_query($sqlSelCodes);
$rowSelCodes = mysql_fetch_array($rstSelCodes);

$first_value="";
if(is_array($rowSelCodes))
{
	$array_keys=array();
	$array_keys=explode(",",$rowSelCodes['Codes']);
	$first_value=$array_keys[0];
	
}
//Om

//print_r($cId);
//exit;
?>
<style type="text/css">
	#sell-form { border:0px solid #CCC; padding:10px; float:left; vertical-align:top; width:93%; margin:2%; }

	#sell-form label{
	float: left;
	width: 130px;
	font-weight: bold;
	}
	
	#sell-form input, select, textarea{	
	width: 280px;
	margin-bottom: 5px;
	font-size:12px;	
	}
	
	#sell-form textarea{
	height: 250px;
	width: 450px;
	font-size:12px;
	}
	
	#sell-form a{
		float:left;
	}
	
	#sell-form #submitbutton{
	margin-left: 130px;
	margin-top: 5px;
	width: 90px;
	}
	
	#sell-form #sellby-div { float:left;  }
	
	#sell-form #sellby-div label { width: 70px; margin-bottom:2px; font-weight:normal; }
	#sell-form #sellby-div input { width: 1px; }
	
	#sell-form br{
	clear: left;
	}

	
</style>
<script language="javascript" src="js/ajaxbase.js" type="text/javascript"></script>
<script language="javascript" src="js/add_row.js" type="text/javascript"></script>
<div id='panel' style="width:950px;">

<table width="100%" border="0">
<tr>
<td>
<div style="width: 950px; float: left; vertical-align: top;">

<div class='mypg_left_out'><? include("mypg_left.php"); ?></div>

<div class="fullbox_out">

<table width="100%" border="0">
  
  <tr>
    <td>
	<div id='panel' style="border:none; width:710px; margin:-16px 0px 0px 5px;">
	<div class="fulldiv" style="margin: 10px 0px 0px 0px;">
	
		<div class="fulldiv">
			<?php include("my_swapen_wholesaler.php"); ?>
		</div>
		
		<div style="width: 700px; margin: 0px 0px 0px 0px;" class="fulldiv">
		<div class="fulldiv" style="margin: 20px 0px 0px 0px;">
			<strong class="orangehead" style="float: left; margin: 0px 10px; padding-top: 8px;">Edit Product</strong>
		</div>
		<div class="fullbox_top">&nbsp;</div>
		<div class="fullbox_repeat">
		<table width="100%" align="center"  border="0" cellspacing="0">
      <tr>
	  	<td><?php if($smsg=="true") { echo "<div id='suc-msg'> Product updated successfully. </div>"; } ?></td>
	  </tr>
      <tr>
        <td height="500"><!-- CODEX technology start here -->
		<form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
           
			  <input type="hidden" name="keyword" id="keyword" value="<?php echo $_REQUEST['editwholeitem'];?>">
				<div id="sell-form">
				 <p class="pro_ptag"><label for="pro_name">Product Code</label>
					<b><input type="text" name="product_code" value="<?php echo $rowSelPro['product_code']; ?>" class="inputbg" /></b></p>
				 <p class="pro_ptag">
				 <label for="pro_name">Product Name</label>
					<b><input type="text" name="pname" value="<?php echo $rowSelPro['product_name']; ?>" class="inputbg" /></b>
				 </p>
				 <p class="pro_ptag">
					  <label for='pro_subcategory'>Category</label>
					  <select name="post_category" id="post_category" class="inputbg">
						<option value="">..Select Category..</option>
						<?php				
						 $sql_shocat="SELECT * FROM tbl_category order by category_name asc";
						 $rst_shocat=mysql_query($sql_shocat);
						 while($row_shocat=mysql_fetch_array($rst_shocat)) {
							 $cat_id=$row_shocat['category_id'];
							 $cat_name=$row_shocat['category_name'];							 
							 
							 ?>
							 <option value="<?php echo $cat_id?>" <?php if($cat_id==$rowSelPro['category_id']){?> selected="selected"<?php }?>><?php echo $cat_name;?></option>
							 <?php
						 }
						?>
					</select>
				</p>
					</span>
				
				<p class="pro_ptag">	
					<label for="pro_description">Description</label>
					<textarea name="pro_description" id="pro_description" class="inputbg"><?php echo $rowSelPro['product_description']; ?></textarea>
				</p>				
				
				<p class="pro_ptag">	
				<label for="pro_image1">Product Price</label>
					<input type="text" name="price" id="price" value="<?php echo $rowSelPro['quantity']; ?>" class="inputbg">
				</p>
				
				<p class="pro_ptag">	
					<label for="pro_image1">Product Image 1</label>
					<input type="file" name="pro_image1" id="pro_image1" class="inputbg">
				</p>
				<p class="pro_ptag">
					<label for="pro_image2">Product Image 2</label>
					<input type="file" name="pro_image2" id="pro_image2" class="inputbg">
				</p>
				<p class="pro_ptag">
					<label for="pro_image3">Product Image 3</label>
					<input type="file" name="pro_image3" id="pro_image3" class="inputbg">
				</p>
				<p class="pro_ptag">
					<label for="pro_image4">Product Image 4</label>
					<input type="file" name="pro_image4" id="pro_image4" class="inputbg">
				</p>
				<p class="pro_ptag">
					<label for="pro_image5">Product Image 5</label>
					<input type="file" name="pro_image5" id="pro_image5" class="inputbg">
				</p>
					<div class="form_field_big">
				<p class="pro_ptag">
							<label for="pro_ship" id="lblpro_ship">Shipping cost</label><br /><br />
							
							<input type="text" style="width:80px;"   name="pro_ship" id="pro_ship"  onchange="projected_profit()" value="$"  onBlur="putIndicator(this,'$')" onFocus="clearIndicator(this,'$')" onClick="clearIndicator(this,'$')" onkeydown="return onlyint(event,2)" class="inputbg" />
				</p>
							
				<p class="pro_ptag">			
							<input type="checkbox" style="width: 15px; float:left;" name="ship_status" id="ship_status"  onclick="funcheckShip()" />
							<label for="ship_status" style="width:100px;">Free Shipping</label>
							<input type="checkbox" style="width: 15px; float:left;" name="localPickup" id="localPickup" value="1" />
							<label for="localPickup" style="width:100px;">Free Local Pickup</label>
							<sup><a href="javascript:void(0);" id="local_pickup_more_info" onclick="show_more_info(2)">more info <img align="absmiddle" src="images/small_yellow_star.jpg" style="border:0px" /></a></sup>
							<span style="float:left; color:red; width:100px; display:none;" id="digit_err2">Digits Only</span>
				</p>
					</div><br>
				
				<p class="pro_ptag">		
					<label for="pro_quantity">Quantity</label>
					<input type="text" style="width:80px;" value="<?php echo $rowSelPro['quantity']; ?>" name="pro_quantity" onblur="javascript:return addRows(this.value);" id="pro_quantity" class="inputbg">
				</p>
				
				<p class="pro_ptag">
					<input type="submit" id="submitbutton" name="submitbutton" value="Submit" class="yellow_btn">
				</p>
					<div class="row_to_clone" id="divReplica">
					<label for="pro_quantity">Product Key</label>
					<input type="text" name="code[0]" id="code[0]" maxlength="10"  size="10" value="<?php echo $first_value; ?>" onblur="javascript:return checkAvailability(this.value,getRef('pro_quantity').value,this,<?php echo $proId;?>);" class="inputbg"  />
					<span id="msg[0]" name="msg[0]" class="error">Please enter the 10 digit code</span>
					</div>
				</div><br>
            
        </form></td>
      </tr>
    </table>
		</div>
		<div class="fullbox_bot">&nbsp;</div>
		</div>
	</div>
	</div>
	</td>
  </tr>
</table>
</div>

</div>
</td>
 </tr>
</table>
</div>
<script language="javascript" type="text/javascript">
addRowsWithValue(<?php echo $rowSelPro['quantity']; ?>,'<?php echo $rowSelCodes['Codes']; ?>');
//add_Sold(<?php echo $rowSelPro['quantity']; ?>,'<?php echo $rowSelCodes['Sold']; ?>');
function show_more_info(a)
{
	
	if(a==2)
	{
		var pos = $('#local_pickup_more_info').position();
		var nleft = parseInt(pos.left);
		var ntop = parseInt(pos.top)+15;
		$('#more_info').css({'left': nleft, 'top':ntop});
		$('#desc_info').html('Local Pick up is an option for members who want to provide buyers an extra option to receive their products directly from the source. The buyer receives a 10 digit code for the pick-up, which he will provide you with for verification of transaction.');
		$('#more_info').fadeIn('slow');
	}
	
	
}
</script>
<div style=" display: none; width: 400px; font-weight:bold; padding:10px; background-color:#cccccc; opacity:0.5;" class="user_popup" id="more_info">
	<span style="float:left; width:100%; text-align:right; margin-bottom:5px;"><label style="cursor:pointer;" onclick="$('#more_info').fadeOut('slow');">x</label></span>
	<span style="float:left; width:100%; margin-bottom:10px;" id="desc_info"></span>
</div>